/*
 * * * * * * * * * * * * * * * * 
 * Author:        魏佳楠
 * CreatTime:  2020/6/22 11:59:27 
 * Description: 
 * * * * * * * * * * * * * * * * 
*/
/*
 * * * * * * * * * * * * * * * * 
 * Author:        赵元恺
 * CreatTime:  2020/7/3 22:47:22 
 * Description: 自定义MyInstantiate，实其生成的游戏物体全部存在MAP的子物体
 * UpDateTime: 2020/7/24 15:17
 * Description: 将地图变为针对不同模式会生成不同类型地图
 * * * * * * * * * * * * * * * * 
*/
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using System.Collections;
using UnityEngine.UI;
using UnityEngine;
using LongZhiJie;

public class ScenseBuildLogic : MonoBehaviour
{
	public int mapx=33;
	public int mapy=21;
	public GameObject[] floors;
	public GameObject[] walls;
	public GameObject[] obstacles;
	public GameObject[] Grasses;
	public GameObject[] trees;
    public GameObject RedSaveBox;
    public GameObject BlueSaveBox;
    public Transform MAP;
	//0表示路面，1表示草丛，2表示障碍物，3无法毁灭障碍物,4宝石生成点，5红方金库,6蓝方金库
	public int[,] maps;
    //map1宝石争霸地图
	public int[,] map1 =
	{
		{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
		{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
		{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
		{0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1,1},
		{0,0,2,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0},
		{0,0,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,2,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,1,1,0,0,0},
		{0,0,0,0,0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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	};
    //map3金库攻防地图
    public int[,] map3 =
    {
        {1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
        {1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
        {1,1,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,1,1},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
        {0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,2,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,1,1,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1,1,1,2,2},
        {0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,2,1,1,1,2,2},
        {1,1,2,0,0,1,1,1,1,2,0,0,0,0,0,0,0,0,1,2,2},
        {1,1,2,0,0,2,1,1,1,2,0,0,0,0,0,0,0,0,0,0,0},
        {1,1,2,0,0,2,1,1,1,2,0,0,0,0,0,0,0,0,0,0,0},
        {1,1,2,0,0,2,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

        {0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0},

        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,2,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,2,1,1,1,2,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,2,1,1,1,2,0,0,0,0,0},
        {2,2,1,0,0,0,0,0,0,0,0,2,0,1,1,1,0,0,0,0,0},
        {2,2,1,1,1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0},
        {2,2,1,1,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,1,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0},
        {0,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0,2,0,0},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
        {1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
        {1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
    };
	//map4金库攻防地图
	public int[,] map4 =
	{
		{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
		{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
		{1,1,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,1,1},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

		{2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2},

		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0},
		{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
		{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
		{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
	};
	public  Dictionary<string, int[,]> mapDictionary;
	public string current_mode;
	public bool InitFinish { get; private set; }
	public void InitData()
	{
		InitFinish = false;

		if (HYLDStaticValue.ModenName == "HYLDTryGame")
			maps = map2;
		else if (HYLDStaticValue.ModenName == "HYLDBaoShiZhengBa")
		{
			maps = map2;
			HYLDStaticValue.ModenName = "HYLDBaoShiZhengBa";
		}
		else if (HYLDStaticValue.ModenName == "HYLDJinKuGongFang")
		{
			maps = map4;
			HYLDStaticValue.ModenName = "HYLDJinKuGongFang";
		}


		int floorl = floors.Length;
		int walll = walls.Length;
		int obstaclel = obstacles.Length;
		int Grassesl = Grasses.Length;
		int treesl = trees.Length;
		for (int i = 0; i < mapx; i++)//33
		{
			for (int j = 0; j < mapy; j++)//21
			{
				if (maps[i, j] == 0||maps[i, j] == 1)
				{
					int random = Random.Range(0,floorl);
					MyInstantiate(floors[random], new Vector3(i - mapx / 2, 0.01f, j - mapy / 2), Quaternion.identity);
				} 
				if (maps[i, j] == 1)//草丛
				{
					int random = Random.Range(0,Grassesl);
					//MyInstantiate(Grasses[random], new Vector3(i - mapx / 2, 0.01f, j - mapy / 2), Quaternion.identity);
				}
				if (maps[i, j] == 2) //障碍物
				{
					int random = Random.Range(0,obstaclel);
					MyInstantiate(obstacles[random], new Vector3(i - mapx / 2, 0f, j - mapy / 2), Quaternion.identity);
					int random2 = Random.Range(0,floorl);
					MyInstantiate(floors[random2], new Vector3(i - mapx / 2, 0.01f, j - mapy / 2), Quaternion.identity);
				}
                if (maps[i, j] == 5) //红方金库
                {
                    int random = Random.Range(0, floorl);
                    MyInstantiate(floors[random], new Vector3(i - mapx / 2, 0.01f, j - mapy / 2), Quaternion.identity);
                    MyInstantiate(RedSaveBox, new Vector3(i - mapx / 2, 0.41f, j - mapy / 2),new Quaternion(-1f,1f,0,0));
                }
                else if(maps[i, j] == 6)//蓝方金库
                {
                    int random = Random.Range(0, floorl);
                    MyInstantiate(floors[random], new Vector3(i - mapx / 2, 0.01f, j - mapy / 2), Quaternion.identity);
                    MyInstantiate(BlueSaveBox, new Vector3(i - mapx / 2, 0.41f, j - mapy / 2), new Quaternion(1f, 1f, 0, 0));
                }
            }
		}
		//生成墙壁
		for (int i = -mapx/2-1; i <= mapx/2+1; i++)
		{
			int random = Random.Range(0, walll - 1); ;
			if (i % 3 == 0)
			{
				random = walll - 1;
			}

			MyInstantiate(walls[random], new Vector3(i, 0, -mapy / 2 - 1), Quaternion.identity);
			MyInstantiate(walls[random], new Vector3(i, 0, mapy / 2 + 1), Quaternion.identity);
		}

		for (int j = -mapy/2; j < mapy/2+1; j++)
		{
			int random = Random.Range(0,walll-1);
			MyInstantiate(walls[random], new Vector3(-mapx / 2 - 1, 0, j), Quaternion.identity);
			MyInstantiate(walls[random], new Vector3(mapx / 2 + 1, 0, j), Quaternion.identity);
		}


		//生成树
		for (int i = -mapx / 2 - 1; i <= mapx / 2 + 1; i++)
		{
			int random = Random.Range(0, treesl - 2); 
			if (i % 3 == 0)
			{
				random = treesl - 2+Random.Range(0,2);
			}

			MyInstantiate(trees[random], new Vector3(i, 0, -mapy / 2 - Random.Range(3,10)), Quaternion.identity);
			MyInstantiate(trees[random], new Vector3(i, 0, mapy / 2 + Random.Range(3, 10)), Quaternion.identity);
		}
		for (int j = -mapy / 2; j < mapy / 2 + 1; j++)
		{
			int random = Random.Range(0, treesl - 2); 
			MyInstantiate(trees[random], new Vector3(-mapx / 2 - Random.Range(3, 10), 0, j), Quaternion.identity);
			MyInstantiate(trees[random], new Vector3(mapx / 2 + Random.Range(3, 10), 0, j), Quaternion.identity);
		}
		for (int i = 0; i < mapx*2; i++) //33
		{
			for (int j = 0; j < mapy*2; j++) //21
			{
				
				if (i-mapx<-mapx/2-1||i-mapx>mapx/2+1||j-mapy<-mapy/2-1||j-mapy>mapy/2+1)
				{
					int random = Random.Range(0,floorl);
					//MyInstantiate(trees[random], new Vector3(i - mapx, 0.01f, j - mapy), Quaternion.identity);
					MyInstantiate(floors[random], new Vector3(i - mapx, 0.01f, j - mapy), Quaternion.identity);

				} 
			}
		}
		//HYLDStaticValue.isloading = false;

		InitFinish = true;
	}
	private void MyInstantiate(GameObject mapPresfab,Vector3 MapPrefabvector3,Quaternion MapPrefabquaternion)
	{
		GameObject go = Instantiate(mapPresfab, MapPrefabvector3, MapPrefabquaternion);
		go.transform.SetParent(MAP);
	}


}

